package com.ruoyi.student.service.impl;

import java.util.List;

import com.ruoyi.student.domain.Student;
import com.ruoyi.student.domain.Studentrepeaterlog;
import com.ruoyi.student.dto.StudentAttentionDto;
import com.ruoyi.student.mapper.StudentMapper;
import com.ruoyi.student.mapper.StudentrepeaterlogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.student.mapper.StudentattentionlogMapper;
import com.ruoyi.student.domain.Studentattentionlog;
import com.ruoyi.student.service.IStudentattentionlogService;

import javax.validation.constraints.AssertFalse;

/**
 * 重点关注Service业务层处理
 * 
 * @author hoyo
 * @date 2022-12-29
 */
@Service
public class StudentattentionlogServiceImpl implements IStudentattentionlogService 
{
    @Autowired
    private StudentattentionlogMapper studentattentionlogMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private StudentrepeaterlogMapper studentrepeaterlogMapper;

    /**
     * 查询重点关注
     * 
     * @param id 重点关注主键
     * @return 重点关注
     */
    @Override
    public Studentattentionlog selectStudentattentionlogById(Long id)
    {
        return studentattentionlogMapper.selectStudentattentionlogById(id);
    }

    /**
     * 查询重点关注列表
     * 
     * @param studentattentionlog 重点关注
     * @return 重点关注
     */
    @Override
    public List<Studentattentionlog> selectStudentattentionlogList(Studentattentionlog studentattentionlog)
    {
        return studentattentionlogMapper.selectStudentattentionlogList(studentattentionlog);
    }

    /**
     * 新增重点关注
     * 
     * @param studentattentionlog 重点关注
     * @return 结果
     */
    @Override
    public int insertStudentattentionlog(Studentattentionlog studentattentionlog)
    {
        return studentattentionlogMapper.insertStudentattentionlog(studentattentionlog);
    }

    /**
     * 修改重点关注
     * 
     * @param studentattentionlog 重点关注
     * @return 结果
     */
    @Override
    public int updateStudentattentionlog(Studentattentionlog studentattentionlog)
    {
        return studentattentionlogMapper.updateStudentattentionlog(studentattentionlog);
    }

    /**
     * 批量删除重点关注
     * 
     * @param ids 需要删除的重点关注主键
     * @return 结果
     */
    @Override
    public int deleteStudentattentionlogByIds(Long[] ids)
    {
        //更新学生面板
        for (Long id : ids) {
            Studentattentionlog studentattentionlog = studentattentionlogMapper.selectStudentattentionlogById(id);
            Student student = studentMapper.selectStudentById(studentattentionlog.getStuSn());
            student.setAttention(0L);
            studentMapper.updateStudent(student);
        }

        return studentattentionlogMapper.deleteStudentattentionlogByIds(ids);
    }

    /**
     * 删除重点关注信息
     * 
     * @param id 重点关注主键
     * @return 结果
     */
    @Override
    public int deleteStudentattentionlogById(Long id,StudentAttentionDto stuDto)
    {
        //更新学生面板
        Long stuSn = stuDto.getStuSn();
        Student student = studentMapper.selectStudentById(stuSn);
        student.setAttention(0L);
        studentMapper.updateStudent(student);
        return studentattentionlogMapper.deleteStudentattentionlogById(id);
    }

    /**
     * 重点关注调用
     * @param
     */
    @Override
    public void saveStudentattentionlog(StudentAttentionDto stuDto) {
        //查看该学生是否在重点关注中
        Long stuSn = stuDto.getStuSn();
        System.out.println(stuDto);
        Studentattentionlog studentattentionlog1 = studentattentionlogMapper.selectStudentattentionlogByStusn(stuSn);
        //如果有数据说明该学生在重点关注中
        if (studentattentionlog1!=null){
            throw new RuntimeException("该名学生已经重点关注了");
        }
        //是否设定为留级
        if(stuDto.getStayclass().equals("是")){
            //填入留级表数据
            Studentrepeaterlog studentrepeaterlog = saveTrepeater(stuDto);
            //保存
            studentrepeaterlogMapper.insertStudentrepeaterlog(studentrepeaterlog);
        }
        //填入重点关注表数据
        Studentattentionlog studentattentionlog = saveAttentionlog(stuDto);
        //保存
        studentattentionlogMapper.insertStudentattentionlog(studentattentionlog);
        //更新学生面板
        Student student = studentMapper.selectStudentById(stuSn);
        student.setAttention(1L);
        studentMapper.updateStudent(student);
    }

    /**
     * 填入重点关注表数据
     * @param stuDto
     * @return
     */
    private Studentattentionlog saveAttentionlog(StudentAttentionDto stuDto) {
        Studentattentionlog studentattentionlog = new Studentattentionlog();
        studentattentionlog.setStuName(stuDto.getStuName());
        studentattentionlog.setStuSn(stuDto.getStuSn());
        studentattentionlog.setTeacherName(stuDto.getTeacherName());
        studentattentionlog.setComment(stuDto.getComment());
        studentattentionlog.setTime(stuDto.getTime());
        studentattentionlog.setDifficultyCoefficient(stuDto.getDifficultyCoefficient());
        studentattentionlog.setStayclass(stuDto.getStayclass());
        return studentattentionlog;
    }

    /**
     * 填入留级表数据
     * @param stuDto
     * @return
     */
    private Studentrepeaterlog saveTrepeater(StudentAttentionDto stuDto) {
        Studentrepeaterlog studentrepeaterlog = new Studentrepeaterlog();
        studentrepeaterlog.setStuSn(stuDto.getStuSn());
        studentrepeaterlog.setTeacherName(stuDto.getTeacherName());
        studentrepeaterlog.setStuName(stuDto.getStuName());
        studentrepeaterlog.setDifficultyCoefficient(stuDto.getDifficultyCoefficient());
        return studentrepeaterlog;
    }
}
